package com.example.mcdonaldapplication.info

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.mcdonaldapplication.R
import com.example.mcdonaldapplication.activity.GoodsDetailActivity
import com.example.mcdonaldapplication.activity.TakeawayActivity
import java.io.BufferedWriter
import java.io.IOException
import java.io.OutputStreamWriter

class GoodsAdapter(private val goodsList: ArrayList<GoodsInfo>, private val from:String, private val context: Activity) : RecyclerView.Adapter<GoodsAdapter.ViewHolder>(){

    class ViewHolder(view: View): RecyclerView.ViewHolder(view){
        val tvName: TextView = view.findViewById(R.id.tvName)
        val tvPrice: TextView = view.findViewById(R.id.tvPrice)
        val tvContent:TextView = view.findViewById(R.id.tvContent)
        val ivImg: ImageView = view.findViewById(R.id.ivImg)
        val btnAddToCart:Button = view.findViewById(R.id.addToCart)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.goods_item,parent,false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        //绑定数据
        val goodsInfo = goodsList[position]
        holder.tvName.text = goodsInfo.name //名称
        holder.tvContent.text = goodsInfo.content
        holder.tvPrice.text = goodsInfo.price
        if (goodsInfo.img == "0"){
            holder.ivImg.setImageResource(R.drawable.hamburger0)
        } else if (goodsInfo.img == "1"){
            holder.ivImg.setImageResource(R.drawable.hamburger1)
        }
        holder.btnAddToCart.setOnClickListener{
            addCart(goodsInfo.gid.toString(), goodsInfo.name, 1, goodsInfo.price, goodsInfo.img)
        }
        //点击商品跳转到商品详情页
        holder.itemView.setOnClickListener {
            Log.d("context", context.toString())
            val intent = Intent(context, GoodsDetailActivity::class.java)
            intent.putExtra("gid",goodsInfo.gid.toString())
            intent.putExtra("name",goodsInfo.name)  //名称
            intent.putExtra("content",goodsInfo.content)    //内容
            intent.putExtra("price", goodsInfo.price)   //单价
            intent.putExtra("img",goodsInfo.img)    //图片
            intent.putExtra("from", from)
            context.finish()
            context.startActivity(intent)
        }
    }

    override fun getItemCount(): Int {
        return goodsList.size
    }

    private fun addCart(gid:String, name:String, num:Int, price:String, img:String) {
        try {
            val prefs = context.getSharedPreferences("cart", Context.MODE_PRIVATE)
            val outputNum = prefs.getInt("${name}num", 0)
            val editor = context.getSharedPreferences("cart", Context.MODE_PRIVATE).edit()
            editor.putString(name, name)
            if(outputNum > 0) {
                editor.putInt("${name}num", outputNum + num)
            }else{
                editor.putInt("${name}num", num)
            }
            editor.putString("${name}price", price)
            editor.putString("${name}img", img)
            editor.putString("${name}gid",gid)
            editor.apply()
        }
        catch (e: IOException){
            e.printStackTrace()
        }
    }
}